import { Effect, Reducer } from 'umi';
import {IExamType,ISubject,IQuestionType} from '@/interface/question';
import {getExamType,getSubject,getQuestionsType,addQuestion} from '@/services/questions';


export interface QuestionModelState{
    examType:IExamType[],
    subjects:ISubject[],
    questionType:IQuestionType[],
    // successInfo:{code:number,msg:string}
}

export interface QuestionModelType{
    namespace:'question',
    state:QuestionModelState,
    effects:{
        getExamType:Effect,
        getSubject:Effect,
        getQuestionsType:Effect,
        addQuestion:Effect
    },
    reducers:{
        save: Reducer<QuestionModelState>;
    }
}

const questionModel:QuestionModelType = {
    namespace:'question',
    state:{
        examType:[],
        subjects:[],
        questionType:[],
        // successInfo:{
        //     code:-1,
        //     msg:''
        // }
    },
    effects:{
        //获取考试类型
        *getExamType({payload},{call,put}){
            let result = yield call(getExamType);
            if(result.code === 1){
                yield put({
                    type:'save',
                    payload:{examType:result.data}
                })
            }
        },
        *getSubject({payload},{call,put}){
            let result = yield call(getSubject);
            if(result.code === 1){
                yield put({
                    type:'save',
                    payload:{subjects:result.data}
                })
            }
        },
        *getQuestionsType({payload},{call,put}){
            let result = yield call(getQuestionsType);
            if(result.code === 1){
                yield put({
                    type:'save',
                    payload:{questionType:result.data}
                })
            }
        },
        *addQuestion({payload},{call,put}){
            let result = yield call(addQuestion,payload);
            return result
            // if(result.code === 1){
            //     yield put({
            //         type:'save',
            //         payload:{successInfo:result}
            //     })
            // }
        }
    },
    reducers:{
        save(state, action) {
            return {
              ...state,
              ...action.payload,
            };
        }
    }
}

export default questionModel